#include "../include/head.h"
//显示当前虚拟工作路径
int do_pwd(socket_t fd_client, char *cur_path){
    //发送结果
    send_train(fd_client,TAG_RESPONSEPWD,cur_path);
    return 0;
}
//显示当前目录下的文件信息
int do_ls(socket_t fd_client, MYSQL *mysql, char *cur_path){
    char command[1024] = {0};//命令
    char errmsg[256] = {0};//错误信息
    char msg[1024] = {0};//结果
    train_t train;
    bzero(&train,sizeof(train));
    int file_id = 0;
    sprintf(command,"select file_id from file where file_path = '%s' and is_deleted = 0;",cur_path);
    int ret = mysql_query(mysql,command);
    if(ret != 0){
        fprintf(stderr, "%s:%s\n", "do_ls select file_id", mysql_error(mysql));
        strcpy(errmsg,"出错了@_@\n");
        send_train(fd_client,TAG_ERROR,errmsg);
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(result);
    file_id = atoi(row[0]);
    mysql_free_result(result);
    bzero(command,sizeof(command));
    sprintf(command,"select file_id,filename,file_type from file where parent_id = %d and is_deleted = 0;",file_id);
    ret = mysql_query(mysql,command);
    if(ret != 0){
        fprintf(stderr, "%s:%s\n", "do_ls select result", mysql_error(mysql));
        strcpy(errmsg,"出错了@_@\n");
        send_train(fd_client,TAG_ERROR,errmsg);
        return -1;
    }
    result = mysql_store_result(mysql);
    // 考虑没有result的情况
    const char *title = "\ninode\ttype\tname\n";
    strcat(msg,title);
    while((row = mysql_fetch_row(result)) != NULL){
        char content[256] = {0};
        sprintf(content,"%d\t%s\t%s\n",atoi(row[0]),row[2],row[1]);
        strcat(msg,content);
    }
    // 发送结束标志
    send_train(fd_client,TAG_RESPONSELS,msg);
    return 0;
}
//实现虚拟工作路径跳转（可以处理 ..到上一级）
int do_cd(socket_t fd_client, MYSQL *mysql, char *cur_path){
    train_t train;
    char temp_path[256] = {0};
    char command[1024] = {0}; 
    char msg[1024] = {0};
    char path_argv[256] = {0};
    int ret = -1;
    MYSQL_RES *result;
    
    //接收客户端发来的预跳转的工作路径
    bzero(&train, sizeof(train_t));
    recv(fd_client, &train.data_length, sizeof(int), MSG_WAITALL);
    recv(fd_client, train.data, train.data_length, MSG_WAITALL);
    strcpy(temp_path, train.data);
    strcpy(path_argv,train.data);

    //正确分割路径
    char *first_dir = strtok(path_argv, "/");
    char *second_dir = strtok(NULL, "");  // 获取剩余部分
                                          
    /* printf("%s\n", second_dir); */
    /* strcpy(second_argv, second_dir); */
    //判断预跳转的路径是否存在，更新cur_path
    if(strcmp(first_dir, "..") != 0){
        //cd dir1 或 cd dir1/dir2 的情况
        bzero(temp_path, sizeof(temp_path));
        strcpy(temp_path, cur_path);
        
        //避免重复的斜杠
        if (strcmp(cur_path, "/") != 0) {
            strcat(temp_path, "/");
        }
        strcat(temp_path, train.data);
        
        sprintf(command, "select file_id from file where file_path = '%s' and file_type = 'd' and is_deleted = 0;", temp_path);
        ret = mysql_query(mysql, command);
        if(ret != 0){
            fprintf(stderr, "%s:%s\n", "mysql_query_do_cd1", mysql_error(mysql));
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "没有那个文件或目录>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        
        result = mysql_store_result(mysql);
        if (mysql_num_rows(result) == 0) {
            mysql_free_result(result);
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "没有那个文件或目录>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        mysql_free_result(result);
        
        strcpy(cur_path, temp_path);
    }else{
        //处理 cd .. 或 cd ../dir1 的情况
        bzero(temp_path, sizeof(temp_path));
        strcpy(temp_path, cur_path);
        
        sprintf(command, "select parent_id from file where file_path = '%s';", temp_path);
        ret = mysql_query(mysql, command);
        if(ret != 0){
            fprintf(stderr, "%s:%s\n", "mysql_query_do_cd2", mysql_error(mysql));
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "查找上一级目录parent_id出错>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        
        result = mysql_store_result(mysql);
        MYSQL_ROW row;
        row = mysql_fetch_row(result);
        if (!row) {
            mysql_free_result(result);
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "找不到当前目录信息>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        
        int parent_id = atoi(row[0]);
        if(parent_id == -1){
            mysql_free_result(result);
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "根目录无法跳转到上一级目录>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        
        mysql_free_result(result);
        bzero(command, sizeof(command));
        
        sprintf(command, "select file_path from file where file_id = %d;", parent_id);
        ret = mysql_query(mysql, command);
        if(ret != 0){
            fprintf(stderr, "%s:%s\n", "mysql_query_do_cd3", mysql_error(mysql));
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "查找上一级目录parent_path出错>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        
        result = mysql_store_result(mysql);
        row = mysql_fetch_row(result);
        if (!row) {
            mysql_free_result(result);
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "找不到父目录路径>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        
        //获取父目录路径
        bzero(temp_path, sizeof(temp_path));
        strcpy(temp_path, row[0]);
        mysql_free_result(result);
        
        //如果有二级目录（如 cd ../dir1）
        if (second_dir && second_dir[0] != '\0') {
            //拼接父目录和二级目录
            char full_path[256] = {0};
            strcpy(full_path, temp_path);
            strcat(full_path, "/");
            strcat(full_path, second_dir);
            
            //检查完整路径是否存在
            bzero(command, sizeof(command));
            sprintf(command, "select file_id from file where file_path = '%s' and file_type = 'd' and is_deleted = 0;", full_path);
            ret = mysql_query(mysql, command);
            result = mysql_store_result(mysql);
            
            if(ret != 0 || mysql_num_rows(result) == 0){
                mysql_free_result(result);
                bzero(&train, sizeof(train_t));
                train.tag = TAG_ERROR;
                sprintf(msg, "没有那个文件或目录>_<\n");
                strcpy(train.data, msg);
                train.data_length = strlen(train.data);
                send(fd_client, &train.tag, sizeof(int), 0);
                send(fd_client, &train.data_length, sizeof(int), 0);
                send(fd_client, train.data, train.data_length, 0);
                return -1;
            }
            mysql_free_result(result);
            strcpy(cur_path, full_path);
        } else {
            //纯 cd .. 的情况
            strcpy(cur_path, temp_path);
            printf("cur_path:%s\n", cur_path);
        }
    }
    
    bzero(&train, sizeof(train_t));
    train.tag = TAG_RESPONSEMKDIR;
    sprintf(msg, "%s", cur_path);
    strcpy(train.data, msg);
    train.data_length = strlen(train.data);
    send(fd_client, &train.tag, sizeof(int), 0);
    send(fd_client, &train.data_length, sizeof(int), 0);
    send(fd_client, train.data, train.data_length, 0);
    printf("跳转成功：当前路径为[%s]\n", cur_path);
    return 0;
}
//实现虚拟工作路径跳转（可以处理 ..到上一级）
int do_cd1(socket_t fd_client, MYSQL *mysql, char *cur_path){
    train_t train;
    char temp_path[256] = {0};
    char command[1024] = {0}; 
    char msg[1024] = {0};
    int ret = -1;
    MYSQL_RES *result;
    //接收客户端发来的预跳转的工作路径
    bzero(&train, sizeof(train_t));
    recv(fd_client, &train.data_length, sizeof(int), MSG_WAITALL);
    recv(fd_client, train.data, train.data_length, MSG_WAITALL);
    strcat(temp_path, train.data);

    //分割出路径的第一个dir是否为 ..
    char *first_dir = strtok(temp_path, "/");
    char *second_dir = first_dir + strlen(first_dir) + 1;
    //判断预跳转的路径是否存在，更新cur_path
    if(strcmp(first_dir, "..") != 0){
        //cd dir1/dir2
        //将temp_path变成虚拟的绝对路径
        bzero(temp_path, sizeof(temp_path));
        strcat(temp_path, cur_path);
        strcat(temp_path, "/");
        strcat(temp_path, train.data);
        sprintf(command, "select file_id from file where file_path = '%s' and file_type = 'd' and is_deleted = 0;", temp_path);
        ret = mysql_query(mysql, command);
        if(ret != 0){
            fprintf(stderr, "%s:%s\n", "mysql_query_do_cd1", mysql_error(mysql));
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "没有那个文件或目录>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        // 保存上一次的查询结果再进行新的查询
        result = mysql_store_result(mysql);
        mysql_free_result(result);
        //将跳转的路径存在，更新cur_path
        strcpy(cur_path, temp_path);
    }else{
        bzero(temp_path, sizeof(temp_path));
        strcat(temp_path, cur_path);
        //cd ../dir1
        //因为要跳转到上一级，也就是查询当前路径cur_path 有没有 parent_id，拒接判断是否是目录和是否被删除（很扯淡）
        sprintf(command, "select parent_id from file where file_path = '%s';", temp_path);
        ret = mysql_query(mysql, command);
        if(ret != 0){
            fprintf(stderr, "%s:%s\n", "mysql_query_do_cd2", mysql_error(mysql));
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "查找上一级目录parent_id出错>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        //还需要判断查找的值是不是-1，如果是根目录不能实现 cd ..
        result = mysql_store_result(mysql);
        MYSQL_ROW row;
        row = mysql_fetch_row(result);
        int parent_id = atoi(row[0]);
        if(parent_id == -1){
            //当前目录是根目录，不支持 cd ..
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "根目录无法跳转到上一级目录>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        //进入下一次查询之前应该先清空结果
        bzero(command, sizeof(command));
        mysql_free_result(result);
        //通过刚创建的parent_id 作为file_id在file 表中获取 路径 file_path
        sprintf(command, "select file_path from file where file_id = %d;", parent_id);
        ret = mysql_query(mysql, command);
        if(ret != 0){
            fprintf(stderr, "%s:%s\n", "mysql_query_do_cd3", mysql_error(mysql));
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "查找上一级目录parent_path出错>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        result = mysql_store_result(mysql);
        row = mysql_fetch_row(result);
        //把父路径存入temp_path，已经完成跳转到上一级目录
        strcpy(temp_path, row[0]);
        if (second_dir && second_dir[0] != '\0'){
            strcpy(temp_path, "/");
            strcpy(temp_path, second_dir);
        }
        //再进行一次查询，看路径是否存在，并修改cur_path
        mysql_free_result(result);
        bzero(command, sizeof(command));
        sprintf(command, "select file_id from file where file_path = '%s' and file_type = 'd' and is_deleted = 0;", temp_path);
        ret = mysql_query(mysql, command);
        if(ret != 0){
            fprintf(stderr, "%s:%s\n", "mysql_query_do_cd4", mysql_error(mysql));
            bzero(&train, sizeof(train_t));
            train.tag = TAG_ERROR;
            sprintf(msg, "没有那个文件或目录>_<\n");
            strcpy(train.data, msg);
            train.data_length = strlen(train.data);
            send(fd_client, &train.tag, sizeof(int), 0);
            send(fd_client, &train.data_length, sizeof(int), 0);
            send(fd_client, train.data, train.data_length, 0);
            return -1;
        }
        //将跳转的路径存在，更新cur_path
        strcpy(cur_path, temp_path);
    }
    bzero(&train, sizeof(train_t));
    train.tag = TAG_RESPONSEMKDIR;
    sprintf(msg, "%s", cur_path);
    strcpy(train.data, msg);
    train.data_length = strlen(train.data);
    send(fd_client, &train.tag, sizeof(int), 0);
    send(fd_client, &train.data_length, sizeof(int), 0);
    send(fd_client, train.data, train.data_length, 0);
    printf("跳转成功：当前路径为[%s]\n", cur_path);
    return 0;
}
//创建目录文件（需要涉及到file表的插入操作）
int do_mkdir(socket_t fd_client, MYSQL *mysql, char *cur_path){
    train_t train;
    char command[1024] = {0};
    char msg[1024] = {0};
    char temp_path[256] = {0};
    char new_filename[256] = {0};
    int user_id = -1;
    int parent_id = -1;
    int ret = -1;
    //首先接收客户端发来的新目录名
    recv(fd_client, &train.data_length, sizeof(int), MSG_WAITALL);
    recv(fd_client, train.data, train.data_length, MSG_WAITALL);
    //把新目录名组成新的路径
    strcat(temp_path, cur_path);
    strcat(temp_path, "/");
    strcat(temp_path, train.data);
    //新目录名要存储
    strcat(new_filename, train.data);
    //在file表中查询有没有同名的目录文件
    sprintf(command, "select file_id from file where file_path = '%s' and is_deleted = 0;", temp_path);
    printf("=%s\n", command);
    ret = mysql_query(mysql, command);
    if(ret != 0){
        fprintf(stderr, "%s:%s\n", "mysql_query_do_mkdir1", mysql_error(mysql));
        bzero(&train, sizeof(train_t));
        train.tag = TAG_ERROR;
        sprintf(msg, "无法创建目录 : 目录文件已存在>_<\n");
        strcpy(train.data, msg);
        train.data_length = strlen(train.data);
        send(fd_client, &train.tag, sizeof(int), 0);
        send(fd_client, &train.data_length, sizeof(int), 0);
        send(fd_client, train.data, train.data_length, 0);
        return -1;
    }
    // 保存上一次的查询结果再进行新的查询
    MYSQL_RES *result = mysql_store_result(mysql);
    //如果不存在同名目录，就在file表中执行插入操作
    //先获得新创建目录的user_id == cur_path对应的user_id  
    bzero(command, sizeof(command));
    sprintf(command, "select file_id,user_id from file where file_path = '%s';", cur_path);
    printf("=%s\n", command);
    ret = mysql_query(mysql, command);
    if(ret != 0){
        fprintf(stderr, "%s:%s\n", "mysql_query_do_mkdir2", mysql_error(mysql));
        bzero(&train, sizeof(train_t));
        train.tag = TAG_ERROR;
        sprintf(msg, "获取上一级目录的user_id失败>_<\n");
        strcpy(train.data, msg);
        train.data_length = strlen(train.data);
        send(fd_client, &train.tag, sizeof(int), 0);
        send(fd_client, &train.data_length, sizeof(int), 0);
        send(fd_client, train.data, train.data_length, 0);
        return -1;
    }
    //存储获得的user_id
    result = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(result);
    parent_id = atoi(row[0]);
    user_id = atoi(row[1]);

    bzero(command, sizeof(command));
    mysql_free_result(result);
    //将新目录文件的信息插入file表中(包括：filename,user_id,file_path,file_type)
    sprintf(command, "insert into file (filename,user_id,parent_id,file_path,file_type) values('%s','%d','%d','%s','%c');", new_filename,user_id,parent_id,temp_path,'d');
    printf("=%s\n", command);
    ret = mysql_query(mysql, command);
    if(ret != 0){
        fprintf(stderr, "%s:%s\n", "mysql_query_do_mkdir3", mysql_error(mysql));
        bzero(&train, sizeof(train_t));
        train.tag = TAG_ERROR;
        sprintf(msg, "目录创建失败>_<\n");
        strcpy(train.data, msg);
        train.data_length = strlen(train.data);
        send(fd_client, &train.tag, sizeof(int), 0);
        send(fd_client, &train.data_length, sizeof(int), 0);
        send(fd_client, train.data, train.data_length, 0);
        return -1;
    }
    bzero(&train, sizeof(train_t));
    train.tag = TAG_RESPONSEMKDIR;
    sprintf(msg, "目录[%s]新建成功!\n", new_filename);
    strcpy(train.data, msg);
    train.data_length = strlen(train.data);
    send(fd_client, &train.tag, sizeof(int), 0);
    send(fd_client, &train.data_length, sizeof(int), 0);
    send(fd_client, train.data, train.data_length, 0);
    printf("目录[%s]新建成功！\n", new_filename);
    return 0;
}
//删除目录文件（需要涉及到file表的更新操作--is_deleted）
int do_rmdir(socket_t fd_client, MYSQL *mysql, char *cur_path){
    train_t train;
    char command[1024] = {0};
    char msg[1024] = {0};
    char temp_path[256] = {0};
    char del_filename[256] = {0};
    int ret = -1;
    //首先接收客户端发来的要删除的目录名
    recv(fd_client, &train.data_length, sizeof(int), MSG_WAITALL);
    recv(fd_client, train.data, train.data_length, MSG_WAITALL);
    //把要删除的目录名组成新的路径
    strcat(temp_path, cur_path);
    strcat(temp_path, "/");
    strcat(temp_path, train.data);
    //存储要删除的文件名
    strcat(del_filename, train.data);
    //在file表中查询有没有同名的目录文件
    sprintf(command, "select file_id from file where file_path = '%s' and is_deleted = 0;", temp_path);
    printf("=%s\n", command);
    ret = mysql_query(mysql, command);
    if(ret != 0){
        fprintf(stderr, "%s:%s\n", "mysql_query_do_rmdir1", mysql_error(mysql));
        bzero(&train, sizeof(train_t));
        train.tag = TAG_ERROR;
        sprintf(msg, "无法删除目录 : 没有那个文件或目录>_<\n");
        strcpy(train.data, msg);
        train.data_length = strlen(train.data);
        send(fd_client, &train.tag, sizeof(int), 0);
        send(fd_client, &train.data_length, sizeof(int), 0);
        send(fd_client, train.data, train.data_length, 0);
        return -1;
    }
    // 保存上一次的查询结果再进行新的查询
    MYSQL_RES *table = mysql_store_result(mysql);
    //如果找到要删除的目录，就在file表中删除
    //先获得新创建目录的user_id == cur_path对应的user_id  
    bzero(command, sizeof(command));
    sprintf(command, "update file set is_deleted = 1 where file_path = '%s';", temp_path);
    printf("=%s\n", command);
    ret = mysql_query(mysql, command);
    if(ret != 0){
        fprintf(stderr, "%s:%s\n", "mysql_query_do_rmdir2", mysql_error(mysql));
        bzero(&train, sizeof(train_t));
        train.tag = TAG_ERROR;
        sprintf(msg, "目录删除失败>_<\n");
        strcpy(train.data, msg);
        train.data_length = strlen(train.data);
        send(fd_client, &train.tag, sizeof(int), 0);
        send(fd_client, &train.data_length, sizeof(int), 0);
        send(fd_client, train.data, train.data_length, 0);
        return -1;
    }
    bzero(&train, sizeof(train_t));
    train.tag = TAG_RESPONSEMKDIR;
    sprintf(msg, "目录[%s]删除成功!\n", del_filename);
    strcpy(train.data, msg);
    train.data_length = strlen(train.data);
    send(fd_client, &train.tag, sizeof(int), 0);
    send(fd_client, &train.data_length, sizeof(int), 0);
    send(fd_client, train.data, train.data_length, 0);
    printf("目录[%s]删除成功!\n", del_filename);
    return 0;
}
//删除文件，仅支持单个文件名
int do_remove(socket_t fd_client, MYSQL *mysql, char *cur_path){
    train_t train;
    memset(&train, 0, sizeof(train_t));
    char train_data[256] = {0};//接收train.data，即文件名
    char temp_path[256] = {0};
    strcat(temp_path, cur_path);// 存储用户当前路径
    char command[1024] = {0};// 进行数据库操作的语句
    char msg[255] = {0};//返回给客户端的信息
    // 接收客户端发来的文件名
    recv(fd_client, &train.data_length, sizeof(int), MSG_WAITALL);
    recv(fd_client, &train.data, train.data_length, MSG_WAITALL);
    strcat(train_data, train.data);
    // 检查文件名是否合法
    int len = strlen(train_data);
    if(train_data[0] == '/' || train_data[len-1] == '/'){
        sprintf(msg, "路径非法，不要在开头和结尾加'\\'\n");                                                                           
        send_train(fd_client, TAG_ERROR, msg);
        return -1; 
    }   
// 查询这个文件是否存在
    strcat(temp_path, "/");
    strcat(temp_path, train_data);
    sprintf(command, "select file_id from file where file_path = '%s' and is_deleted = 0 and file_type = '-'", temp_path);
    if(mysql_query(mysql, command) != 0){ 
        fprintf(stderr, "查询语句不规范，失败：%s\n", mysql_error(mysql));
        memset(msg, 0, sizeof(msg));
        sprintf(msg, "服务端发生错误，疑似查询语句不规范：%s\n", mysql_error(mysql));
        send_train(fd_client, TAG_ERROR, msg);
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        fprintf(stderr, "获取结果集失败：%s\n", mysql_error(mysql));
        memset(msg, 0, sizeof(msg));
        sprintf(msg, "服务端发生错误，获取结果集失败：%s\n", mysql_error(mysql));
        send_train(fd_client, TAG_ERROR, msg);
        return -1;
    }
    // 遍历结果行
    MYSQL_ROW row;
    row = mysql_fetch_row(result);
    if(row == NULL){
        fprintf(stderr,"查询结果不存在：%s\n", mysql_error(mysql));
        memset(msg, 0, sizeof(msg));
        sprintf(msg, "文件不存在：%s\n", mysql_error(mysql));
        send_train(fd_client, TAG_ERROR, msg);
        return -1;
    }
 // 到这一步，得到查询结果file_id，进行文件修改
    memset(command, 0, sizeof(command));
    sprintf(command, "update file set is_deleted = 1 where file_id = '%d'", atoi(row[0]));
    if(mysql_query(mysql, command) != 0){
        fprintf(stderr, "更新失败%s\n", mysql_error(mysql));
        memset(msg, 0, sizeof(msg));
        sprintf(msg, "服务端发生错误，疑似数据更新失败：%s\n", mysql_error(mysql));
        send_train(fd_client, TAG_ERROR, msg);
        return -1;
    }
    memset(msg, 0, sizeof(msg));
    sprintf(msg, "删除操作成功\n");
    send_train(fd_client, TAG_RESPONSEREMOVE, msg);
    printf("删除操作成功\n");
    mysql_free_result(result);
    return 0;
}
